package com.resifind.www.servlet.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author 36069
 */
@WebFilter("/admin/*")
public class AdminFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String[] urls = {"/admin/checkLogin", "/admin/login"};
        String url = req.getRequestURL().toString();
        for (String u : urls) {
            if (url.contains(u)) {
                chain.doFilter(request, response);
                return;
            }
        }
        Object password = req.getSession().getAttribute("admin");
        if (password == null) {
            resp.sendRedirect(req.getContextPath() + "/index.html");
        } else {
            chain.doFilter(request, response);
        }
    }
}